//
//  SimpleNetworkTests.swift
//  SimpleNetworkTests
//
//  Created by 谭淇 on 15/3/4.
//  Copyright (c) 2015年 谭淇. All rights reserved.
//

import UIKit
import XCTest

// 断言的提示信息可以省略。(但不建议省略，可读性！)

class SimpleNetworkTests: XCTestCase {
    
    // 网络访问工具
    let net = SimpleNetwork()
    // 测试网络地址
    let urlString = "http://httpbin.org/get"
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func testRequestJSON() {
        // 测试错误的网络请求
        net.requestJSON(.GET, "", nil) { (result, error) -> () in
            println(error)
            XCTAssertNotNil(error, "必须返回错误")
        }
        
        // 测试返回json
///  没有输出原因：网络访问是异步的。在Xcode6.0之前，异步的单元测试相当难
///  Xcode4.0的时候，苹果都不好意思把单元测试暴露出来！
        
        // 1.定义一个“期望” == 描述异步的需求，只是一个标记而已
        let expectation = expectationWithDescription(urlString)
        net.requestJSON(.GET, urlString, nil) { (result, error) -> () in
            println(result)
            // 2.标记“期望”达成
            expectation.fulfill()
        }
        // 3.等待期望达成 / 10s时间到
        // 参数时间：等待异步操作必须在10s内完成
        waitForExpectationsWithTimeout(10, handler: { (error) -> Void in
            XCTAssertNil(error, "期望失败")
        })
    }
    
    ///  测试网络请求
    func testRequest() {
        /// GET
        // 测试nil
        var result = net.request(.GET, "", nil)
        XCTAssertNil(result, "结果返回应该为nil")
        
        result = net.request(.GET, urlString, nil)
        XCTAssertNotNil(result, "请求应该被建立")
        XCTAssert(result?.URL?.absoluteString == urlString, "返回的URL不正确")
        result = net.request(.GET, urlString, ["name": "zhangsan"])
        XCTAssert(result?.URL?.absoluteString == urlString + "?name=zhangsan", "返回的URL不正确")
        
        /// POST
        // 测试nil
        result = net.request(.POST, urlString, nil)
        XCTAssertNil(result, "请求应该为nil")
        
        result = net.request(.POST, urlString, ["name": "zhangsan"])
        // 测试方法
        XCTAssert(result?.HTTPMethod == "POST", "请求方法不正确")
        // 测试数据体
        XCTAssert(result?.HTTPBody == "name=zhangsan".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true), "数据体不正确")
    }
    
    ///   测试queryString函数
    func testQueryString() {
        // 测试nil
        XCTAssertNil(net.queryString(nil), "结果应该为nil")
        // 测试一个元素字典
        XCTAssert(net.queryString(["name": "zhangsan"]) == "name=zhangsan", "结果不正确")
        // 测试两个元素字典
        XCTAssert(net.queryString(["name": "zhangsan", "age": "18"])! == "age=18&name=zhangsan", "结果不正确")
        // 测试百分号转义
        println(net.queryString(["name": "张三", "age": "18", "book": "ios 8.0"])!)
        XCTAssert(net.queryString(["name": "张三", "age": "18", "book": "ios 8.0"])! == "age=18&book=ios%208.0&name=%E5%BC%A0%E4%B8%89", "结果不正确")
    }
    
}
